<template>
  <div id="app">
    <div class="container">
      <h1 class="title">Message Board</h1>
        <form action="/api/messages" method="POST" @submit.prevent="addMessage">
          <div class="form-group">
            <input type="text" class="form-control" name="name" placeholder="Name" v-model="name">
          </div>
          <div class="form-group">
            <textarea class="form-control" rows="5" placeholder="Say something..." v-model="text"></textarea>
          </div>
          <button class="btn btn-default">Submit</button>
        </form>
        <div class="messages-header">{{ messages.length }} messages</div>
        <div class="messages">
          <ul tansition="fade">
            <template v-for="message in messages">
              <message-item :message="message"></message-item>
            </template>
          </ul>
        </div>
    </div>
  </div>
</template>

<script>

import MessageItem from './components/MessageItem.vue'
// npm install --save axios
var axios = require('axios');

export default {
  name: 'app',
  data () {
    return {
      name: '',
      text: '',

      // 测试数据
      messages: [
        {name: 'Jack', text: 'Awesome', created_at: '2017-06-29T11:04:52.226Z'},
        {name: 'Tom', text: 'I like it', created_at: '2017-06-19T11:04:52.226Z'},
        {name: 'Alex', text: 'Good job!', created_at: '2017-06-09T11:04:52.226Z'},
      ]
    }
  },
  methods: {
    // onSubmit() {
    //   this.messages.unshift({
    //     name: this.name,
    //     text: this.text,
    //     created_at: new Date().toISOString()
    //   })
    //   this.name = ''
    //   this.text = ''
    // },
    addMessage() {
      axios.post('/api/messages', {
          'name': this.name,
          'text': this.text
          }).then(response => {
              if (response.data.ok) {
                this.messages.unshift({
                  'name': this.name,
                  'text': this.text,
                  'created_at': new Date().toISOString()
              })
          }
      })
    }
  },
  mounted() {
    axios.get('/api/messages').then(response => this.messages =response.data)
  },
  components: {
    MessageItem
  }
}
</script>

<style>
.container {
  width: 650px;
  margin-top: 60px;
}

#app {
  margin-top: 60px;
  margin: 0 auto;
}

.title {
  text-align: center;
  margin-bottom: 20px;
  color: #e3e3e3;
}

.messages-header {
  line-height: 1;
  color: #666;
  padding: 20px 0 6px;
  border-bottom: 1px solid #eee;
  text-transform: uppercase;
  font-size: 13px;
}

.messages ul {
  margin: 0;
  padding: 0;
}
</style>